/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.classes.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.exception.TrainingServiceException;
import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.classes.entity.ClassesPlanEntity;
import org.springblade.modules.classes.vo.ClassesPlanVO;
import org.springblade.modules.classes.excel.ClassesPlanExcel;
import org.springblade.modules.classes.wrapper.ClassesPlanWrapper;
import org.springblade.modules.classes.service.IClassesPlanService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import springfox.documentation.annotations.ApiIgnore;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * 班级培训计划 控制器
 *
 * @author junxin
 * @since 2023-04-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-classesPlan/classesPlan")
@Api(value = "班级培训计划", tags = "班级培训计划接口")
public class ClassesPlanController extends BladeController {

	private final IClassesPlanService classesPlanService;

	/**
	 * 班级培训计划 详情
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入classesPlan")
	public R<ClassesPlanVO> detail(ClassesPlanEntity classesPlan) {
		ClassesPlanEntity detail = classesPlanService.getOne(Condition.getQueryWrapper(classesPlan));
		return R.data(ClassesPlanWrapper.build().entityVO(detail));
	}

	/**
	 * 班级培训计划 自定义分页
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入classesPlan")
	public R<IPage<ClassesPlanVO>> page(ClassesPlanVO classesPlan, Query query) {
		IPage<ClassesPlanVO> pages = classesPlanService.selectClassesPlanPage(Condition.getPage(query), classesPlan);
		return R.data(pages);
	}

	/**
	 * 班级培训计划 新增
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入classesPlan")
	@ServiceLog(value = TrainingOperateConstant.CLASSES,operate = TrainingOperateConstant.ADD,describe = "班级培训计划新增")
	public R save(@Valid @RequestBody ClassesPlanEntity classesPlan) {

		ClassesPlanEntity oldDataEntity = classesPlanService.getOne(Wrappers.<ClassesPlanEntity>query().lambda()
			.eq(ClassesPlanEntity::getClassesId, classesPlan.getClassesId())
		);
		if (null!=oldDataEntity)throw new TrainingServiceException("该班级已有培训计划,请先删除");
		ClassesPlanEntity classesPlanEntity = classesPlanService.getOne(Wrappers.<ClassesPlanEntity>query().lambda()
			.eq(ClassesPlanEntity::getClassesId, classesPlan.getClassesId())
			.eq(ClassesPlanEntity::getPlanId, classesPlan.getPlanId())
		);
		if (null==classesPlanEntity)classesPlanService.save(classesPlan);
		return R.success("操作成功");
	}

	/**
	 * 班级培训计划 修改
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入classesPlan")
	@ServiceLog(value = TrainingOperateConstant.CLASSES,operate = TrainingOperateConstant.UPDATE,describe = "班级培训计划修改")
	public R update(@Valid @RequestBody ClassesPlanEntity classesPlan) {
		return R.status(classesPlanService.updateById(classesPlan));
	}


	/**
	 * 班级培训计划 删除
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ServiceLog(value = TrainingOperateConstant.CLASSES,operate = TrainingOperateConstant.DELETE,describe = "班级培训计划删除")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(classesPlanService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/export-classesPlan")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入classesPlan")
	public void exportClassesPlan(@ApiIgnore @RequestParam Map<String, Object> classesPlan, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<ClassesPlanEntity> queryWrapper = Condition.getQueryWrapper(classesPlan, ClassesPlanEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(ClassesPlan::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(ClassesPlanEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<ClassesPlanExcel> list = classesPlanService.exportClassesPlan(queryWrapper);
		ExcelUtil.export(response, "班级培训计划数据" + DateUtil.time(), "班级培训计划数据表", list, ClassesPlanExcel.class);
	}

}
